#include "Service.h"
#include <json/json.h>
#include <cstdio>
#include <iostream>


Service::Service() : m_redis(sw::redis::Redis("tcp://127.0.0.1:6379")), m_e(), m_u(1000, 9999)
{
}



int Service::postLogin(HttpRequest* req, HttpResponse* rsp)
{
	rsp->SetHeader("access-Control-Allow-Origin", "*");
	Json::Value value;
	Json::Reader reader;
	std::cout << req->Body() << std::endl;
	if (reader.parse(req->Body(), value)) {
		std::string check = value["check"].asString();
		auto res = m_redis.get(check);
		if (res) {
			return rsp->String("loginSuccess!");
		}
		else {
			return rsp->String("checkError!");
		}
	}
	else {
		return rsp->String("loginError!");
	}
}

int Service::getCheck(HttpRequest* req, HttpResponse* rsp)
{

	rsp->SetHeader("access-Control-Allow-Origin", "*");
	//creq->Host();
	int check = m_u(m_e);
	m_redis.setex(std::to_string(check), 600, "true");
	rsp->json["check"] = std::to_string(check);
	return 200;
}

int Service::getPing(HttpRequest* req, HttpResponse* rsp)
{
	return rsp->String("123321123");
}
